package org.jugile.util;

/*

Copyright (C) 2007-2011 Jukka Rahkonen  email: jukka.rahkonen@iki.fi

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

*/

import java.io.FileReader;
import java.io.InputStream;
import java.util.Properties;


/**
 * ""
 * 
 * @author jukka.rahkonen@iki.fi
 */
public class Props extends Jugile {

	private static Properties props = null;
	private static Properties props() {
		if (props != null) return props;
		try {
			System.out.println("initializing props()");
			Props foo = new Props();
			Class cl = foo.getClass();
			ClassLoader cloader = cl.getClassLoader();
			InputStream is = cloader.getResourceAsStream("jugile.properties");
			props = new Properties();
			if (filename != null) {
				System.out.println("Loading props from file: " + filename);
				props.load(new FileReader(filename));
			} else {
				props.load(is);
			}
			return props;
		} catch (Exception e) {
			U.log("could not read jugile.properties" + ((filename!=null)?": "+filename:""));
			props = new Properties();
			return props;
		}
	}
	
	private static String filename = null;
	public static void filename(String fname) { filename = fname; props = null; }
	
	public static String get(String key) {
		// try first our new PropNode - prepare projects for this first
		//String p = PropNode.get(key);
		//if (p != null) return p;
		return props().getProperty(key); 
	}
	public static void set(String key, String value) { props().setProperty(key, value); }
	public static String get(String key, String def) {
		String res = get(key);
		if (empty(res)) return def;
		return res;
	}
	
	public static int getInt(String key) { return parseIntSafe(get(key)); }
	public static boolean is(String key) { return parseBoolSafe(get(key)); } 
	
	// ----- fixed property getters -----
	
	private static Boolean isProduction = null;
	public synchronized static boolean isProduction() {
		if (isProduction != null) return isProduction;
		if ("true".equals(get("jugile.isProduction"))) isProduction = true;
		else isProduction = false;
		return isProduction;
	}
	public synchronized static void setIsProduction(boolean v) { isProduction = v; }

	private static Boolean isAdminNode = null;
	public synchronized static boolean isAdminNode() {
		if (isAdminNode != null) return isAdminNode;
		if ("true".equals(get("jugile.isAdminNode"))) isAdminNode = true;
		else isAdminNode = false;
		return isAdminNode;
	}
	public synchronized static void setIsAdminNode(boolean v) { isAdminNode = v; }
	
	private static Boolean isStateless = null;
	public synchronized static boolean isStateless() {
		if (isStateless != null) return isStateless;
		if ("true".equals(get("jugile.isStateless"))) isStateless = true;
		else isStateless = false;
		return isStateless;
	}
	public synchronized static void setIsStateless(boolean v) { isStateless = v; }

	public static String getDbFileName() {
		String fname = get("jugile.db.file");
		if (!empty(fname)) return fname;
		return System.getProperty("user.home")+ "/" + getPackageName() + ".db4o";
	}

	public static String getPackageName() {
		return get("jugile.package");
	}
	
	public static String getWebPackage() {
		return getPackageName() + ".web";
	}
	
	public static String getDomainClassName() {
		if (eq(get("jugile.nodomain"),"true")) return null;
		if (empty(getPackageName())) return null;
		return getPackageName() + ".domain.Domain";
	}
	
	public static String getTemplatePath() {
		String tmplpath = get("jugile.web.tmpl");
		if (!empty(tmplpath)) return tmplpath;
		return getWebPackage().replace('.','/');
	}
	

	// ----- db proprety defaults -------
	
	public static String getDbName() {
		String dbname = get("jugile.db.name");
		if (dbname == null) return "jugile";
		return dbname;
	}
	
	public static String getDbUser() {
		String dbuser = get("jugile.db.user");
		if (dbuser == null) return "jugile";
		return dbuser;
	}
	public static String getDbPasswd() {
		String dbpw = get("jugile.db.passwd");
		if (dbpw == null) return "jugile";
		return dbpw;
	}
	public static String getDbHost() {
		String host = get("jugile.db.host");
		if (host == null) return "localhost";
		return host;
	}

}
